unit uGlassMain;

interface
uses Classes, SysUtils, Windows, uGlassMisc;
Const
            FILEEXTENSION_MANUAL    =     '.man';

            // The size AND Order of this array must be maintained, new additions to be
            // appended only to it. Otherwise GetParamType will be out of whack
            SUPPORTED_PARAMS        :     array[0..4] of string = ('-enable','-disable',
                                          '-on','-off', '-not');

            // a match representing the element in the array should be added here too.
            // The handler is in the ProcessCommandLine function

Type        TSupportedParams        =     (spEnable, spDisable, spOn, spOff, spNot);


          PROCEDURE Init();
          FUNCTION IsParamSupported(aParam : string) : boolean;
          FUNCTION ProcessCommandLine() : boolean;
          PROCEDURE ShowHelp();
          FUNCTION GetParamType(aParam : String) : TSupportedParams;



implementation
Var
  IsGlass : boolean;
FUNCTION GetParamType(aParam : String) : TSupportedParams;
Begin
// The order or the items in the array must be maintained
Result := spNot;
if aParam = SUPPORTED_PARAMS[0] then Result := spEnable;
if aParam = SUPPORTED_PARAMS[1] then Result := spDisable;
if aParam = SUPPORTED_PARAMS[2] then Result := spOn;
if aParam = SUPPORTED_PARAMS[3] then Result := spOff;
if aParam = SUPPORTED_PARAMS[4] then Result := spNot

End;

FUNCTION IsParamSupported(aParam : String) : boolean;
Var
  i   :   integer;
Begin
for i := Low(SUPPORTED_PARAMS) to High(SUPPORTED_PARAMS) do
     Begin
       if aParam = SUPPORTED_PARAMS[i] then
          Begin
            Result := True;
            Exit;
          End;
     End;
Result := False;
End;

// =============================================================================
PROCEDURE Init();
Begin
if ProcessCommandLine then
   Begin

   End
else
   Begin
     WriteLn ('Type ' + ExtractFileName(ParamStr(0)) + ' -h for help');
   End;
End;
// =============================================================================



// =============================================================================
FUNCTION ProcessCommandLine() : boolean;
Var
    i       :     integer;
    TempList:     TStringList;
    OptionsOk : boolean;
Begin
OptionsOk := false;
if ParamCount > 0 then
   Begin
   TempList := TStringList.Create;

   // do something
   Result := True;
   for i := 1 to ParamCount do
        Begin
        if ParamStr(i) = '-h' then
            Begin
            WriteLn(ParamStr(i));
            ShowHelp;
            Result := False;
            Exit;
            End;

        if IsParamSupported(ParamStr(i)) = true then
           Begin
             // do some further processing for this paramater
             case GetParamType(ParamStr(i)) of
                  spEnable       :     Begin
                                       IsGlass := True;
                                       DWM_EnableBlurBehind(GetConsoleWindow(), True);
                                       OptionsOk := True;
                                       End;
                  spDisable      :     Begin
                                       IsGlass := False;
                                       DWM_EnableBlurBehind(GetConsoleWindow(), False);
                                       OptionsOk := True;
                                       End;
                  spOn           :     Begin
                                       IsGlass := True;
                                       DWM_EnableBlurBehind(GetConsoleWindow(), True);
                                       OptionsOk := True;
                                       End;
                  spOff          :     Begin
                                       IsGlass := True;
                                       DWM_EnableBlurBehind(GetConsoleWindow(), False);
                                       OptionsOk := True;
                                       End;
                  spNot           :    Begin
                                       IsGlass := not IsGlass;
                                       DWM_EnableBlurBehind(GetConsoleWindow(), IsGlass);
                                       End;
             end;
           if IsGlass then WriteLn('Glass is: On') else WriteLn('Glass is: Off');
           End
        else
           Begin
           if not OptionsOk then
              Begin
              WriteLn('Unsupported Parameter "' + ParamStr(i) + '" passed to ' + ExtractFileName(ParamStr(0)));
              Result := False;
              End;
           End;
        End;
    TempList.Free;
   End
ELSE
   Begin
     Result := True;
     // Return true here, because there are no paramaters to parse but nothing
     // has gone wrong - should do something usefull here
     ShowHelp();
   End;
End;
// =============================================================================




// =============================================================================
PROCEDURE ShowHelp();
Var
  aManualFile : TextFile;
  aText       : string;
  i           : integer;
Begin
  // if a Manual exists under the exe's filename + manual extension then
  // show that file instead, else, show basic help
  if FileExists(ExtractFileName(ParamStr(0)) + FILEEXTENSION_MANUAL)then
     Begin
      try
          Begin
          AssignFile(aManualFile, ParamStr(0) + FILEEXTENSION_MANUAL);
          Reset(aManualFile);
          while not Eof(aManualFile) do
           begin
            ReadLn(aManualFile, aText);
            WriteLn(aText);
           End;
           CloseFile(aManualFile);
          End;
      Except
          Begin
          WriteLn('');
          WriteLn('Problem while trying to open file ' + ParamStr(0) + FILEEXTENSION_MANUAL);
          WriteLn('');
          End;
      end;
     End
  ELSE
      Begin
        // Show basic help
        WriteLn('Basic Commandline Usage for ' + ExtractFileName(ParamStr(0)));
        WriteLn('');
        WriteLn('   Type: ');
        WriteLn('       ' + ExtractFileName(ParamStr(0)) + ' -h          :     basic help');
        WriteLn('       ' + ExtractFileName(ParamStr(0)) + ' <param>=<data>');
        WriteLn('');
        WriteLn('There should have existed a simple manual file for ' + ExtractFileName(ParamStr(0)));
        WriteLn('but it was not found');
        WRiteLn('');
        WriteLn('As a result, what you see above is only basic usage instructions for passing');
        WriteLn('paramaters to this program and not an actual manual');
        WriteLn('');
        WriteLn('Internal List of Supported Commands: ');
        WriteLn('');
        for i := Low(SUPPORTED_PARAMS) to High(SUPPORTED_PARAMS) do
             Begin
             WriteLn('      ' + SUPPORTED_PARAMS[i]);
             End;
      End;
End;
// =============================================================================
end.
